/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.oim.fx.ui.list;

import javafx.geometry.Insets;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.PixelReader;
import javafx.scene.image.PixelWriter;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;

/**
 *
 * @author XiaHui
 */
public class HeadImageItemPane extends StackPane {

	private final StackPane basePane = new StackPane();
	private final Rectangle baseClip = new Rectangle();

	private final StackPane showPane = new StackPane();

	private final StackPane imagePane = new StackPane();
	private final ImageView imageView = new ImageView();
	private final Rectangle imageClip = new Rectangle();

	private WritableImage grayImage = null;
	private Image normalImage = null;
	private boolean gray = false;

	public HeadImageItemPane() {
		initComponent();
		iniEvent();
	}

	private void initComponent() {
		this.getChildren().add(basePane);

		imagePane.setStyle("-fx-background-color:rgba(255,255,255,0.1)");
		imagePane.setClip(imageClip);
		imagePane.getChildren().add(imageView);

		showPane.setPadding(new Insets(2, 2, 2, 2));
		showPane.getStyleClass().add("head-image-show");
		showPane.getChildren().add(imagePane);

		basePane.setClip(baseClip);
		basePane.setPadding(new Insets(1, 1, 1, 1));
		basePane.getStyleClass().add("head-image-pane");
		basePane.getChildren().add(showPane);

	}

	private void iniEvent() {
//		StringBuilder style=new StringBuilder();
//		style.append("-fx-border-color: #4e8fe2;");
//		style.append("-fx-border-radius:5");
//		style.append("-fx-border-width:1;");
//		basePane.setOnMouseExited(m -> {
//			basePane.setStyle("-fx-border-color:null;");
//		});
//		basePane.setOnMouseEntered(m -> {
//			basePane.setStyle(style.toString());
//		});
	}

	public void setHeadSize(double value) {

		baseClip.setWidth(value);
		baseClip.setHeight(value);

		imageView.setFitHeight(value - 6);
		imageView.setFitWidth(value - 6);

		imageClip.setWidth(value - 6);
		imageClip.setHeight(value - 6);
	}

	public void setHeadRadius(double value) {
		baseClip.setArcHeight(value);
		baseClip.setArcWidth(value);

		imageClip.setArcHeight(value - 6);
		imageClip.setArcWidth(value - 6);
	}

	public void setHeadImage(Image image) {
		this.normalImage = image;
		imageView.setImage(image);
		setGrayImage(image);
	}

	private void setGrayImage(Image image) {
		PixelReader pixelReader = image.getPixelReader();
		grayImage = new WritableImage((int) image.getWidth(), (int) image.getHeight());
		PixelWriter pixelWriter = grayImage.getPixelWriter();

		for (int y = 0; y < image.getHeight(); y++) {
			for (int x = 0; x < image.getWidth(); x++) {
				Color color = pixelReader.getColor(x, y);
				color = color.grayscale();
				pixelWriter.setColor(x, y, color);
			}
		}
	}

	public boolean isGray() {
		return gray;
	}

	public void setGray(boolean gray) {
		this.gray = gray;
		if (gray) {
			imageView.setImage(grayImage);
		} else {
			imageView.setImage(normalImage);
		}
	}
}
